﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using Newtonsoft.Json;
using Public_Class_Library;
using Q.M.Finance.Domain.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using W.M.Materiel.Application.IServices;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.IRepository;

namespace W.M.Materiel.Application.Services
{
    internal class Purchase_order_tableServices : BaseServices<purchase_order_table, string>, IPurchase_order_tableServices
    {
        private readonly IPurchase_order_tableRepository purchase_Order_TableRepository;
        private readonly Cross_service_Helper cross_Service_Helper;
        public Purchase_order_tableServices(Cross_service_Helper cross_Service_Helper,IPurchase_order_tableRepository purchase_Order_TableRepository) : base(purchase_Order_TableRepository)
        {
            this.purchase_Order_TableRepository = purchase_Order_TableRepository;
            this.cross_Service_Helper = cross_Service_Helper;
        }

        /// <summary>
        /// 采购显示+分页+查询
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="id">查询条件:采购单价</param>
        /// <returns>统一返回格式</returns>
        public async Task<PageETO<purchase_order_table>> GetPurchase_Order_Table(int pageSize,int pageIndex,string? id)
        {
            PageETO<purchase_order_table> pageETO = new PageETO<purchase_order_table>();
            var list = await purchase_Order_TableRepository.GetShowInforAsync();
            if (!string.IsNullOrEmpty(id))
            {
                list = list.Where(x => x.purchase_code.Contains(id)).ToList();
            }
            pageETO.TotalCount= list.Count;
            pageETO.PageCount = (int)Math.Ceiling(pageETO.TotalCount * 1.0 / pageSize);
            pageETO.Data = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageETO;
        }
        /// <summary>
        /// 反填采购订单
        /// </summary>
        /// <param name="salesid"></param>
        /// <returns></returns>
        public async Task<purchase_order_table> FindsalesInfo(string? purchase_code)
        {
            Expression<Func<purchase_order_table, bool>> i = c => c.purchase_code == purchase_code;
            var sales = purchase_Order_TableRepository.GetShowObj(i);
            return sales;

        }
        /// <summary>
        /// 根据采购单号反填收款方信息
        /// </summary>
        /// <param name="salesid"></param>
        /// <returns></returns>
        public recipient_information GetInfo(string purchase_code)
        {
            var res1 = cross_Service_Helper.Get("http://10.31.59.4:8023/api/Recipient_informationWrite/GetpayerInfo").Result;
            var obj = JsonConvert.DeserializeObject<List<recipient_information>>(res1);
            var list = purchase_Order_TableRepository.GetShowInfor();
            var res = (from a in list
                       join b in obj on a.supplier_name equals b.payee
                       where a.purchase_code == purchase_code
                       select b).FirstOrDefault();
            return res;
        }
    }
}
